package hector.tang.controller;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/file")
public class FileController {

    @RequestMapping("/upload")
    public String upload(HttpServletRequest req) throws Exception {
        System.out.println("文件上传");
        String path=req.getSession().getServletContext().getRealPath("/uploads");
        System.out.println("上传路径："+path);

        //
        File file=new File(path);
        if(!file.exists()){
            file.mkdir();
        }

        //解析req 拿到上传的文件项
        DiskFileItemFactory factory=new DiskFileItemFactory();

        ServletFileUpload upload=new ServletFileUpload(factory);

        //解析 拿到文件项列表
        List<FileItem> itemList=upload.parseRequest(req);
        for(FileItem item:itemList){
            //判断 item对象是否是上传文件项
            if(item.isFormField()){
                //普通表单项
            }else{
                //上传文件项
                String filename=item.getName();
                String id=UUID.randomUUID().toString().replace("-","");
                filename=id+"_"+filename;
                item.write(new File(path,filename));
                System.out.println("写入文件");
                //删除临时文件
                item.delete();
            }
        }

        return "success";
    }

    /**
     * springmvc 上传文件
     *
     * <form action="file/upload" method="post" enctype="multipart/form-data">
     *     选择文件：<input type="file" name="upload">    MultipartFile upload  表单上传文件name 和方法参数名必须一致
     *     <input type="submit" value="上传">
     * </form>
     * @return
     */
    @RequestMapping("/upload2")
    public String upload2(HttpServletRequest req, MultipartFile upload) throws Exception{
        System.out.println("upload2 文件上传");
        String path=req.getSession().getServletContext().getRealPath("/uploads");
        System.out.println("上传路径："+path);

        //
        File file=new File(path);
        if(!file.exists()){
            file.mkdir();
        }
        String filename=upload.getOriginalFilename();

        String id=UUID.randomUUID().toString().replace("-","");
        filename=id+"_"+filename;

        upload.transferTo(new File(path,filename));

        return "success";
    }

    /**
     * 上传文件到别的服务器 接收文件服务器端只需要有一个springvmc项目和对应的保存路径就行
     * @param upload
     * @return
     * @throws Exception
     */
    @RequestMapping("/upload3")
    public String upload3(MultipartFile upload) throws Exception{
        System.out.println("跨服文件上传");
        String path="http://loaclhost:8080/uploads/";
        System.out.println("上传路径："+path);

        //
        String filename=upload.getOriginalFilename();
        String id=UUID.randomUUID().toString().replace("-","");
        filename=id+"_"+filename;

        //创建客户端
        Client client = Client.create();
        WebResource webResource=client.resource(path+filename);

        webResource.put(upload.getBytes());

        return "success";
    }
}
